package com.example.monitoringsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.Insert;
import com.example.monitoringsystem.bean.Health;
import com.example.monitoringsystem.bean.User;
import com.example.monitoringsystem.mapper.HealthMapper;
import com.example.monitoringsystem.service.HealthService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;

@Service
public class HealthServiceImpl implements HealthService {
    @Autowired
    private HealthMapper healthMapper;

    @Override
    public void insertHealthInformation(Health health) {
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        health.setId(currentUser.getId());
        healthMapper.insert(health);
    }

    //获取健康状态
    @Override
    public Health selectHealthInformation(){
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        LocalDate today = LocalDate.now();
        QueryWrapper<Health> wrapper = new QueryWrapper<Health>();
        wrapper.eq("id", currentUser.getId());
        wrapper.eq("date",today);
        Health health = healthMapper.selectOne(wrapper);
        if(health != null){
            return health;
        }
        else {
            System.out.println("今日健康状态还未上传");
            return null;
        }
    }
}
